package Metier.Requete;

import java.util.ArrayList;
import java.util.List;

public class TableRequete {

	private String select;
	private String from;
	private String where;
	private int index;
	private boolean etatFrom;
	private List<String> listeWhere;

	public TableRequete(String select, String from, String where, int index, boolean eFrom) {
		this.select = select;
		this.from = from;
		this.index = index;
		this.etatFrom = eFrom;
		if (!select.equals("*"))
		setWhere(where);
		else
			this.where = "";
	}

	public String getSelect()
	{
		return "t"+index+"."+select;
	}

	public String getFrom()
	{
		return from;
	}
	public String getNomTable()
	{
		return this.from;
	}

	public int getIndex() {
		return this.index;
	}

	public boolean getEtatFrom() {
		return this.etatFrom;
	}

	public void setWhere(String text) {
		listeWhere = new ArrayList<String>(); 
		if (!text.equals(""))
		{
			test(text);
			aff();
		}else
			where = "";
	}

	public void test(String text)
	{
		int indexOr = text.indexOf(" or ");
		int indexAnd= text.indexOf(" and ");
		if ((indexOr == -1)&&(indexAnd == -1))
		{
			this.listeWhere.add(text);
		}
		else
		{
			if((indexOr < indexAnd))
			{
				String[] tabText = text.split(" or ",1);
			}

			if((indexOr == -1))
			{
				String[] tabText = text.split(" and ");
				int len = tabText[0].length();
				String text0 = (String) text.subSequence(0, len+5);
				String text1 = (String) text.subSequence(len+5,text.length());
				this.listeWhere.add(text0);
				test(text1);
			}

		}
	}

	public void aff()
	{
		String wh="";
		for(int i=0;i<this.listeWhere.size();i++)
		{
			wh+=this.getSelect()+this.listeWhere.get(i);
		}
		this.where = wh;
	}

	public String getWhere() {
		return this.where;
	}

}
